package com.education.teach.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dn.res.wideget.CustomLoadMoreView;
import com.dn.res.wideget.DnToolbar;
import com.education.teach.R;
import com.education.teach.di.component.DaggerCounselComponent;
import com.education.teach.di.module.CounselModule;
import com.education.teach.mvp.contract.CounselContract;
import com.education.teach.mvp.model.bean.CounselBean;
import com.education.teach.mvp.model.bean.LoginBean;
import com.education.teach.mvp.presenter.CounselPresenter;
import com.education.teach.mvp.ui.adapter.CounselAdapter;
import com.education.teach.utils.DnCommonUtils;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.utils.ArmsUtils;

import org.simple.eventbus.Subscriber;
import org.simple.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import dn.easy.common.commonutils.TimeUtil;
import dn.easy.common.commonwidget.AVLoadingDialog;
import dn.easy.common.core.EventBusHub;
import dn.easy.common.event.EventBusMsg;

import static com.jess.arms.utils.Preconditions.checkNotNull;

// 咨询问答
public class CounselActivity extends BaseActivity<CounselPresenter> implements CounselContract.View, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.toolbar)
    DnToolbar mToolbar;
    @BindView(R.id.lv_content)
    RecyclerView mRecyclerView;
    @BindView(R.id.swf_pager_list)
    SwipeRefreshLayout mSwipeRereshLayout;
    int maxRow = 10;
    int page = 1;
    private LoginBean mLoginBean;
    private List<CounselBean.ResultBean.QueryListBean> mList = new ArrayList<>();
    private CounselAdapter mAdapter;

    @Inject
    IRepositoryManager mIRepositoryManager;


    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerCounselComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .counselModule(new CounselModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_counsel; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mToolbar.setMainTitle("咨询答疑");
        mToolbar.setToolbarLeftBackImageRes(R.mipmap.top_back);
        mToolbar.setOnLeftTitleClickListener(view -> finish());
        mToolbar.setMainTitleRightText("提问");
//        mToolbar.setMainTitleRightColor(R.color.app_white);

        mToolbar.setOnRightTitleClickListener(v -> launchActivity(new Intent(CounselActivity.this, AddCounselActivity.class)));

        mLoginBean = DnCommonUtils.getUserModel(this);
        mSwipeRereshLayout.setOnRefreshListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new CounselAdapter(R.layout.item_counsel, mList);
        CustomLoadMoreView mLoadMoreView = new CustomLoadMoreView();
        mAdapter.setLoadMoreView(mLoadMoreView);
        mAdapter.setOnLoadMoreListener(this, mRecyclerView);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        mAdapter.isFirstOnly(true);
        mRecyclerView.setAdapter(mAdapter);

        mPresenter.consulQuestionList(mLoginBean.getResult().getXlh(), page, maxRow, true);

        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            List<CounselBean.ResultBean.QueryListBean> listBeans = mAdapter.getData();
            Intent intent = new Intent(CounselActivity.this, CounselDetailActivity.class);
            intent.putExtra("title", Html.fromHtml(listBeans.get(position).getContent()) + "");
            intent.putExtra("name", listBeans.get(position).getYhName());
            intent.putExtra("id", listBeans.get(position).getId() + "");
            intent.putExtra("createName", listBeans.get(position).getCreateName());
            intent.putExtra("date", TimeUtil.formatData(TimeUtil.dateFormatMDofChinese, listBeans.get(position).getCreateDate()));
            launchActivity(intent);
        });

        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            Intent intent = new Intent(CounselActivity.this, AddCounselActivity.class);
            launchActivity(intent);
        });
    }

    @Override
    public void showLoading() {
        AVLoadingDialog.showDialogForLoading(this);
    }

    @Override
    public void hideLoading() {
        if (mSwipeRereshLayout != null) mSwipeRereshLayout.setRefreshing(false);
        AVLoadingDialog.cancelDialogForLoading();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void onRefresh() {
        page = 1;
        mPresenter.consulQuestionList(mLoginBean.getResult().getXlh(), page, maxRow, true);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        mPresenter.consulQuestionList(mLoginBean.getResult().getXlh(), page, maxRow, false);
    }

    @Override
    public void returnRefreshQuestionList(List<CounselBean.ResultBean.QueryListBean> queryList) {
        if (mSwipeRereshLayout != null) mSwipeRereshLayout.setRefreshing(false);
        if (queryList != null)
            mAdapter.setNewData(queryList);
        if (queryList == null || queryList.size() < maxRow)
            mAdapter.loadMoreEnd(true);
    }

    @Override
    public void returnLoadMoreQuestionList(List<CounselBean.ResultBean.QueryListBean> queryList) {
        mAdapter.loadMoreComplete();
        mAdapter.addData(queryList == null ? new ArrayList<>() : queryList);
    }

    @Subscriber(tag = "DO_REFRESH", mode = ThreadMode.MAIN)
    public void doRefresh(String event) {
        if ("refresh".equals(event) && mPresenter != null)
            mPresenter.consulQuestionList(mLoginBean.getResult().getXlh(), page, maxRow, true);
    }
}
